﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MW.Domain;
using MW.Persistence;
using IBatisNet.DataMapper.Exceptions;

namespace MW.Persistence.MapperDao
{
    public class SequenceSqlMapDao : BaseSqlMapDao
    {
        public int GetNextId(string name)
        {
            Sequence sequence = new Sequence(name, -1);

            sequence = ExecuteQueryForObject("GetSequence", sequence) as Sequence;
            if (sequence == null)
            {
                throw new DataMapperException("Error: A null sequence was returned from the database (could not get next " + name + " sequence).");
            }
            object parameterObject = new Sequence(name, sequence.NextId + 1);
            ExecuteUpdate("UpdateSequence", parameterObject);

            return sequence.NextId;
        }
    }
}
